package org.theSeed.springDatasouceProp.pojo;

import cn.hutool.core.collection.CollectionUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.theSeed.dataSource.contract.DataSourceProp;
import org.theSeed.springDatasouceProp.contract.BaseDataSourceAddAware;
import org.theSeed.springDatasouceProp.pojo.prop.SeedDataSourceProp;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义数据源核心配置
 */
@Slf4j
@Component
public class SeedBaseDataSourceHolder {
    @PostConstruct
    public void init(){
        log.info("SeedBaseDataSourceHolder初始化");
        //简单数据源初始化
        dataSourceMap.putAll(seedDataSourceProp.getDataSourceMap());

        //复杂数据源初始化
        if (CollectionUtil.isNotEmpty(baseDataSourceAddAwareList)){
            log.info("复杂基础数据源初始化");
            for (BaseDataSourceAddAware baseDataSourceAddAware : baseDataSourceAddAwareList) {
                baseDataSourceAddAware.addNameDataSource();
            }
            dataSourceMap.putAll(seedDataSourceProp.getDataSourceMapComplex());
            log.info("复杂基础数据源初始化成功");
        }
    }

    @Autowired
    @Getter
    private SeedDataSourceProp seedDataSourceProp;

    /**
     * 自定义基础数据源
     */
    @Autowired(required = false)
    private List<BaseDataSourceAddAware> baseDataSourceAddAwareList;

    /**
     * 全量数据源
     */
    @Getter
    private Map<String,DataSource> dataSourceMap = new ConcurrentHashMap<>();
}
